package com.carrental.api.oms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrental.api.common.exception.Assert;
import com.carrental.common.exception.OperationException;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.common.utils.CommonQueryWrapper;
import com.carrental.api.oms.domain.OmsLendCheck;
import com.carrental.api.oms.domain.bo.OmsLendCheckPost;
import com.carrental.api.oms.mapper.OmsLendCheckMapper;
import com.carrental.api.oms.service.IOmsLendCheckService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-07 23:12:02
 */
@Service
public class OmsLendCheckServiceImpl extends ServiceImpl<OmsLendCheckMapper, OmsLendCheck> implements IOmsLendCheckService {

    /**
     * 获取分页
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public IPage<OmsLendCheck> pageList(CommonQueryParam commonQueryParam) {
        return this.baseMapper.pageList(
                new Page<>(commonQueryParam.getCurrent(), commonQueryParam.getSize()),
                new CommonQueryWrapper<OmsLendCheck>(commonQueryParam).lambda()
                        .eq(OmsLendCheck::getIsDeleted, false)
        );
    }

    /**
     * 添加出车检查单
     *
     * @param lendCheckPost 出车检查单参数
     * @return 是否成功
     */
    @Override
    public Boolean create(OmsLendCheckPost lendCheckPost) {
        if(this.detailByOrderId(lendCheckPost.getOrderId())!=null){
            throw new OperationException("已有出车检查单！");
        }
        OmsLendCheck lendCheck=new OmsLendCheck();
        BeanUtils.copyProperties(lendCheckPost,lendCheck);
        if (!this.save(lendCheck)) {
            throw new OperationException("创建出车检查单失败！");
        }
        return true;
    }

    /**
     * 逻辑删除
     *
     * @param id id
     * @return 是否成功
     */
    @Override
    public Boolean delete(Long id) {
        //先查询，再更新
        OmsLendCheck lendCheck = this.getById(id);
        Assert.notNull(lendCheck, "删除失败，无此出车检查单！");
        lendCheck.setIsDeleted(true);
        if (!this.updateById(lendCheck)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }



    /**
     * 修改出车检查单信息
     * @param id id
     * @param lendCheckPost 出车检查单参数
     * @return 是否成功
     */
    @Override
    public Boolean update(Long id, OmsLendCheckPost lendCheckPost) {
        //先查询，再更新
        OmsLendCheck lendCheck = this.getById(id);
        Assert.notNull(lendCheck, "修改失败，无此出车检查单！");
        BeanUtils.copyProperties(lendCheckPost, lendCheck);
        if (!this.updateById(lendCheck)) {
            throw new OperationException("添加出车检查单信息失败！");
        }
        return true;
    }

    /**
     * 查看出车检查单信息
     *
     * @param id 出车检查单参数
     * @return 出车检查单信息
     */
    @Override
    public OmsLendCheck detail(Long id) {
        OmsLendCheck lendCheck=this.getById(id);
        Assert.notNull(lendCheck,"查询失败，无此出车检查单");
        return lendCheck;
    }

    /**
     * 根绝订单号查看出车检查单信息
     *
     * @param orderId 出车检查单参数
     * @return 出车检查单信息
     */
    @Override
    public OmsLendCheck detailByOrderId(Long orderId) {
        return this.getOne(
                new QueryWrapper<OmsLendCheck>().lambda()
                        .eq(OmsLendCheck::getOrderId,orderId)
                        .eq(OmsLendCheck::getIsDeleted,false)
        );
    }

    /**
     * 获取列表
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public List<OmsLendCheck> list(CommonQueryParam commonQueryParam) {
        return this.list(
                new CommonQueryWrapper<OmsLendCheck>(commonQueryParam).lambda()
                        .eq(OmsLendCheck::getIsDeleted, false)
        );
    }

}
